package com.toune.dltools.ui

import android.os.Bundle
import com.blankj.rxbus.RxBus
import com.qmuiteam.qmui.widget.dialog.QMUIBottomSheet
import com.toune.dltools.R
import com.toune.dltools.adapter.DLAdapterTool
import com.toune.dltools.adapter.DLFormAdapter
import com.toune.dltools.bean.DLCreateFormBean
import com.toune.dltools.bean.DLCreateFormBean.*
import com.toune.dltools.dialog.form.FormCallBack
import com.toune.dltools.dialog.form.SingleInputFormDialog
import com.toune.dltools.model.CreateFormModel
import kotlinx.android.synthetic.main.activity_create_form.*

/**
 * 表单制作界面
 */
class CreateFormActivity : DLBaseActivity<CreateFormModel>() {

    override val layout: Int
        get() = R.layout.activity_create_form
    override val titleStr: String?
        get() = "创建表单"

    var bottomFormTypeDialog: QMUIBottomSheet? = null
    override fun init(savedInstanceState: Bundle?) {
        RxBus.getDefault().subscribe(this, object : RxBus.Callback<DLCreateFormBean>() {
            override fun onEvent(formBean: DLCreateFormBean?) {
                createFormBeans.add(formBean!!)
                setAdapter()
            }
        })
        formAddFAB.setOnClickListener {
            showBottomFormTypeDialog()
        }
    }

    val createFormBeans: MutableList<DLCreateFormBean> = ArrayList()
    private fun showBottomFormTypeDialog() {
        if (bottomFormTypeDialog == null) {
            val bottomFormTypeDialogBuild = QMUIBottomSheet.BottomListSheetBuilder(context)
            bottomFormTypeDialogBuild.addItem(
                "文本框", "$SINGLE_INPUT"
            )
            bottomFormTypeDialogBuild.addItem(
                "多行文本框", "$MUL_INPUT"
            )
            bottomFormTypeDialogBuild.addItem(
                "密码框", "$PSW_INPUT"
            )
            bottomFormTypeDialogBuild.addItem(
                "下拉框", "$SPINNER"
            )
            bottomFormTypeDialogBuild.addItem(
                "纯文本", "$TEXT"
            )
            bottomFormTypeDialogBuild.addItem(
                "单选", "$SINGLE_CHECK"
            )
            bottomFormTypeDialogBuild.addItem(
                "复选框", "$MUL_CHECK"
            )
            bottomFormTypeDialogBuild.addItem(
                "切换按钮", "$SWITCH"
            )
            bottomFormTypeDialogBuild.addItem(
                "日期选择", "$DATE"
            )
            bottomFormTypeDialogBuild.addItem(
                "位置选择", "$LOCATION"
            )
            bottomFormTypeDialogBuild.addItem(
                "按钮", "$BUTTON"
            )
            bottomFormTypeDialogBuild.setOnSheetItemClickListener { dialog, itemView, position, tag ->
                when (tag) {
                    "$SINGLE_INPUT" -> {
                        SingleInputFormDialog(
                            context, R.layout.dialog_create_form, tag, object : FormCallBack {
                                override fun onCallBack(formBean: DLCreateFormBean) {
                                    createFormBeans.add(formBean)
                                    setAdapter()
                                }
                            }).show()
                    }
                    "$MUL_INPUT" -> {
                        SingleInputFormDialog(
                            context, R.layout.dialog_create_form, tag, object : FormCallBack {
                                override fun onCallBack(formBean: DLCreateFormBean) {
                                    createFormBeans.add(formBean)
                                    setAdapter()
                                }
                            }).show()
                    }
                    "$PSW_INPUT" -> {
                        SingleInputFormDialog(
                            context, R.layout.dialog_create_form, tag, object : FormCallBack {
                                override fun onCallBack(formBean: DLCreateFormBean) {
                                    createFormBeans.add(formBean)
                                    setAdapter()
                                }
                            }).show()
                    }
                    "$SPINNER" -> {
                        val bundle = Bundle()
                        bundle.putString("tag", tag)
                        startToActivity(DLAddFormListItemActivity::class.java, bundle)
                    }
                    "$TEXT" -> {
                        SingleInputFormDialog(
                            context, R.layout.dialog_create_form, tag, object : FormCallBack {
                                override fun onCallBack(formBean: DLCreateFormBean) {
                                    createFormBeans.add(formBean)
                                    setAdapter()
                                }
                            }).show()
                    }
                    "$SINGLE_CHECK" -> {
                        val bundle = Bundle()
                        bundle.putString("tag", tag)
                        startToActivity(DLAddFormListItemActivity::class.java, bundle)
                    }
                    "$MUL_CHECK" -> {
                        val bundle = Bundle()
                        bundle.putString("tag", tag)
                        startToActivity(DLAddFormListItemActivity::class.java, bundle)
                    }
                    "$SWITCH" -> {
                        SingleInputFormDialog(
                            context, R.layout.dialog_create_form, tag, object : FormCallBack {
                                override fun onCallBack(formBean: DLCreateFormBean) {
                                    createFormBeans.add(formBean)
                                    setAdapter()
                                }
                            }).show()
                    }
                    "$DATE" -> {
                        SingleInputFormDialog(
                            context, R.layout.dialog_create_form, tag, object : FormCallBack {
                                override fun onCallBack(formBean: DLCreateFormBean) {
                                    createFormBeans.add(formBean)
                                    setAdapter()
                                }
                            }).show()
                    }
                    "$LOCATION" -> {

                    }
                    "$BUTTON" -> {

                    }
                }
                dialog.dismiss()
            }
            bottomFormTypeDialog = bottomFormTypeDialogBuild.build()
            bottomFormTypeDialog!!.show()
        } else {
            bottomFormTypeDialog!!.show()
        }
    }

    var formAdapter: DLFormAdapter? = null
    private fun setAdapter() {
        if (formAdapter == null) {
            formAdapter = DLFormAdapter(createFormBeans)
            val dlAdapterTool = DLAdapterTool<DLCreateFormBean>(context, rcy_view, formAdapter!!)
        } else {
            formAdapter!!.notifyDataSetChanged()
        }
    }

    override fun initEventAndData() {

    }

}